using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NCT.Framework;
using System.Data;
using System.Data.Common;
using NCT.Framework.Database;

namespace VISS.API.Users {
	class UserContactedDA {

        private const string SP_CountByUserKey = "UserContacted_CountByUserKey";
		private const string SP_Delete = "UserContacted_Delete";
        private const string SP_GetByUserKeyWithPaging = "UserContacted_GetByUserKeyWithPaging";
		private const string SP_GetByUserKeyAllowedUserKey = "UserContacted_GetByUserKeyAllowedUserKey";
		private const string SP_Insert = "UserContacted_Insert";

		public static UserContactedEnt CreateFromReader(IDataReader reader) {
			UserContactedEnt item = new UserContactedEnt();
			item.UserKey = ConvertUtils.ToString(reader["UserKey"]);
			item.AllowedUserKey = ConvertUtils.ToString(reader["AllowedUserKey"]);
			item.DateCreated = ConvertUtils.ToDateTime(reader["DateCreated"]);
            item.AllowedUserName = ConvertUtils.ToString(reader["AllowedUserName"]);
            item.AllowedUserGender = (EUserGender)ConvertUtils.ToByte(reader["AllowedUserGender"]);
            item.AllowedUserBirthDate = ConvertUtils.ToDateTime(reader["AllowedUserBirthDate"]);
            item.AllowedUserCityID = ConvertUtils.ToShort(reader["AllowedUserCityID"]);
            item.AllowedUserCity = ConvertUtils.ToString(reader["AllowedUserCity"]);
            item.AllowedUserCountryID = ConvertUtils.ToShort(reader["AllowedUserCountryID"]);
            item.AllowedUserCountry = ConvertUtils.ToString(reader["AllowedUserCountry"]);
            item.AllowedUserEducationID = ConvertUtils.ToShort(reader["AllowedUserEducationID"]);
            item.AllowedUserEducation = ConvertUtils.ToString(reader["AllowedUserEducation"]);
            item.AllowedUserJobID = ConvertUtils.ToShort(reader["AllowedUserJobID"]);
            item.AllowedUserJob = ConvertUtils.ToString(reader["AllowedUserJob"]);
            item.AllowedUserImageCount = ConvertUtils.ToInt(reader["AllowedUserImageCount"]);
            item.AllowedUserBlast = ConvertUtils.ToString(reader["AllowedUserBlast"]);
            item.AllowedUserAvatarFileName = ConvertUtils.ToString(reader["AllowedUserAvatarFileName"]);
            item.AllowedUserAvatarImageStoreID = ConvertUtils.ToInt(reader["AllowedUserAvatarImageStoreID"]);
            item.AllowedUserAvatarStatus = (EUserAvatarStatus)ConvertUtils.ToByte(reader["AllowedUserAvatarStatus"]);
            return item;
		}

        public static int CountByUserKey(string userKey, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
            int result = 0;
            Database db = Database.CreateDatabase(connectionName);
            using (DbCommand cmd = db.CreateStoredCommand(SP_CountByUserKey)) {
                db.AddParameter(cmd, "UserKey", userKey);
                using (IDataReader reader = db.ExecuteReader(cmd)) {
                    if (reader.Read()) {
                        result = ConvertUtils.ToInt(reader["Count"]);
                    }
                }
            }
            return result;
        }

        public static int Delete(string userKey, string alllowedUserKey, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
			int result = -1;
			Database db = Database.CreateDatabase(connectionName);
			using (DbCommand cmd = db.CreateStoredCommand(SP_Delete)) {
				db.AddParameter(cmd, "UserKey", userKey);
				db.AddParameter(cmd, "AllowedUserKey", alllowedUserKey);
				if (dbCon != null && dbTrans != null) {
					result = db.ExecuteNonQuery(cmd, dbCon, dbTrans);
				}
				else {
					result = db.ExecuteNonQuery(cmd);
				}
			}
			return result;
		}

        public static List<UserContactedEnt> GetByUserKeyWithPaging(string userKey, long startRow, long endRow, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
			List<UserContactedEnt> result = new List<UserContactedEnt>();
			Database db = Database.CreateDatabase(connectionName);
			using (DbCommand cmd = db.CreateStoredCommand(SP_GetByUserKeyWithPaging)) {
                db.AddParameter(cmd, "UserKey", userKey);
				db.AddParameter(cmd, "StartRow", startRow);
				db.AddParameter(cmd, "EndRow", endRow);
				using (IDataReader reader = db.ExecuteReader(cmd)) {
					while (reader.Read()) {
						result.Add(CreateFromReader(reader));
					}
				}
			}
			return result;
		}

        public static UserContactedEnt GetByUserKeyAlllowedUserKey(string userKey, string alllowedUserKey, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
			UserContactedEnt result = null;
			Database db = Database.CreateDatabase(connectionName);
			using (DbCommand cmd = db.CreateStoredCommand(SP_GetByUserKeyAllowedUserKey)) {
				db.AddParameter(cmd, "UserKey", userKey);
				db.AddParameter(cmd, "AllowedUserKey", alllowedUserKey);
				using (IDataReader reader = db.ExecuteReader(cmd)) {
					if (reader.Read()) {
						result = CreateFromReader(reader);
					}
				}
			}
			return result;
		}

		public static int Insert(UserContactedEnt item, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
			int result = -1;
            Database db = Database.CreateDatabase(connectionName);
			using (DbCommand cmd = db.CreateStoredCommand(SP_Insert)) {
				db.AddParameter(cmd, "UserKey", item.UserKey);
				db.AddParameter(cmd, "AllowedUserKey", item.AllowedUserKey);
				db.AddParameter(cmd, "DateCreated", item.DateCreated);
				if (dbCon != null && dbTrans != null) {
					result = db.ExecuteNonQuery(cmd, dbCon, dbTrans);
				}
				else {
					result = db.ExecuteNonQuery(cmd);
				}
			}
			return result;
		}

	}
}